package com.lzy.product.apis.impl;

import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.tools.commons.BigDecimalUtil;
import com.lzy.product.apis.StatisticsServiceApi;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.BaseDomain;
import com.lzy.product.dtos.OrderProductDto;
import com.lzy.product.dtos.statistics.*;
import com.lzy.product.service.OrderProductService;
import com.lzy.product.service.OrderProfitRecordService;
import com.lzy.product.service.OrderProjectRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 统计业务服务
 * Created by WIN7 on 2017/12/7.
 */
@Service
public class StatisticsServiceApiImpl implements StatisticsServiceApi {

    @Autowired
    private OrderProfitRecordService orderProfitRecordService;
    @Autowired
    private OrderProductService orderProductService;
    @Autowired
    private OrderProjectRecordService orderProjectRecordService;

    @Override
    public StatisticsVoDto queryStatisticsView() {
        //总销售额
        BigDecimal totalSale = BigDecimalUtil.to2Point(orderProductService.queryTotalSale(null));
        //总收益
        BigDecimal totalProfit = BigDecimalUtil.to2Point(BigDecimalUtil.subtract(totalSale, orderProfitRecordService.getUserTotalProfit()));
        //总订单量
        Integer count = orderProductService.countList(null);
        //总计产生的龙珠
        Integer totalDragon = queryTotalProject(BusinessType.ProjectType.DRAGON.getCode());
        //总计产生的房车券
        Integer totalCarCoupon = queryTotalProject(BusinessType.ProjectType.CAR_COUPON.getCode());
        //总计产生的股权
        Integer totalStockRights = queryTotalProject(BusinessType.ProjectType.STOCK_RIGHTS.getCode());
        StatisticsVoDto voDto = new StatisticsVoDto();
        voDto.setTotalSale(totalSale.toString());
        voDto.setTotalProfit(totalProfit.toString());
        voDto.setCount(count);
        voDto.setDragon(totalDragon);
        voDto.setCarCoupon(totalCarCoupon);
        voDto.setStockRights(totalStockRights);
        return voDto;
    }

    @Override
    public PageInfo<TotalProfitVoDto> queryTotalProfitList(Pager pager, BaseDomain paramDto) {
        PageInfo<TotalProfitVoDto> pageInfo = orderProfitRecordService.queryTotalProfitList(pager, paramDto);
        if (pageInfo.getList() != null) {
            for (TotalProfitVoDto voDto : pageInfo.getList()) {
                //统计订单量
                OrderProductDto param = new OrderProductDto();
                param.setCreateUser(voDto.getUserId());
                voDto.setCount(orderProductService.countList(param));
            }
        }
        return pageInfo;
    }

    @Override
    public PersonalProfitVoDto queryPersonalProfit(Long userId) {
        PersonalProfitVoDto personalProfitVoDto = new PersonalProfitVoDto();
        personalProfitVoDto.setUserId(userId);
        //总销售额
        BigDecimal totalSale = BigDecimalUtil.to2Point(orderProductService.queryTotalSale(userId));
        personalProfitVoDto.setTotalSale(totalSale.toString());

        //总收益额
        DayProfitParamDto paramDto = new DayProfitParamDto();
        paramDto.setUserId(userId);
        personalProfitVoDto.setTotalProfit(getQueryProfit(paramDto));
        return personalProfitVoDto;
    }

    @Override
    public PageInfo<DayProfitVoDto> queryDayProfitList(Pager pager, DayProfitParamDto paramDto) {
        PageInfo<DayProfitVoDto> pageInfo = orderProfitRecordService.queryDayProfitList(pager, paramDto);
        //设置当天完成订单数
        setCountOfDayProfit(pageInfo.getList(), paramDto.getUserId());
        return pageInfo;
    }

    @Override
    public List<DayProfitVoDto> queryDayProfitListNoPage(DayProfitParamDto paramDto) {
        List<DayProfitVoDto> list = orderProfitRecordService.queryDayProfitListNoPage(paramDto);
        //设置当天完成订单数
        setCountOfDayProfit(list, paramDto.getUserId());
        return list;
    }

    @Override
    public PageInfo<OrderProjectRecordVoDto> queryDayProjectList(Pager pager, OrderProjectParamDto paramDto) {
        //购买成功产生的产品包含项有效
        paramDto.setStatus(BusinessType.OrderType.PAYSUCCESS.getCode());
        return orderProjectRecordService.queryDayProjectList(pager, paramDto);
    }

    @Override
    public Integer queryTotalProject(Integer projectType) {
        OrderProjectParamDto paramDto = new OrderProjectParamDto();
        paramDto.setProjectType(projectType);
        return getQueryProject(paramDto);
    }

    @Override
    public String getQueryProfit(DayProfitParamDto paramDto) {
        return BigDecimalUtil.to2Point(orderProfitRecordService.getQueryProfit(paramDto)).toString();
    }

    @Override
    public Integer getQueryProject(OrderProjectParamDto paramDto) {
        //购买成功产生的产品包含项有效
        paramDto.setStatus(BusinessType.OrderType.PAYSUCCESS.getCode());
        return orderProjectRecordService.getQueryProject(paramDto);
    }

    private void setCountOfDayProfit(List<DayProfitVoDto> list, Long userId) {
        if (list != null) {
            for (DayProfitVoDto voDto : list) {
                //统计订单量
                OrderProductDto param = new OrderProductDto();
                param.setCreateUser(userId);
                param.setPayTime(voDto.getDate());
                voDto.setCount(orderProductService.countList(param));
            }
        }
    }
}
